<template>
    <div class="edu-page-warp">
        <condition-card @reset="resetQuery" @search="searchData">
            <!--事件-->
            <el-row>
                <el-col :span="8">
                    <el-form-item :label="$t('minor.major.minorGrade')">
                        <el-date-picker
                            v-model="queryParams.entranceTerm"
                            value-format="yyyy"
                            type="year"
                            clearable
                            :placeholder="$t('message.plsSelect')"
                            style="width: 100%"
                        />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item :label="$t('minor.table.advancedLevel')">
                        <DictSelect
                            v-model="queryParams.advancedLevelCode"
                            :data="options.advancedLevel"
                            clearable
                            style="width: 100%;"
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item :label="$t('minor.major.college')">
                        <DictSelect
                            @change="majorNameChange"
                            v-model="queryParams.departmentId"
                            :data="options.college"
                            :disabled="!canSelectFaculty"
                            :clearable="true"
                            :filterable="true"
                            style="width: 100%;"
                        ></DictSelect>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item :label="$t('minor.major.majorName')">
                        <DictSelect
                            v-model="queryParams.major"
                            :data="options.majorName"
                            :clearable="true"
                            :filterable="true"
                            style="width:100%;"
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item :label="$t('minor.table.startTime')">
                        <el-date-picker
                            v-model="queryParams.startTime"
                            type="date"
                            style="width: 100%;"
                            placeholder="选择日期"
                        ></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item :label="$t('minor.table.endTime')">
                        <el-date-picker
                            v-model="queryParams.endTime"
                            type="date"
                            style="width: 100%;"
                            placeholder="选择日期"
                        ></el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>
        </condition-card>
        <el-card shadow="never">
            <div class="title">微专业培养计划列表</div>
            <div class="btn">
                <div style="float:left;padding: 16px 0px;">
                    <el-input
                        placeholder="请输入学号或姓名搜索"
                        prefix-icon="el-icon-search"
                        v-model="queryParams.studentName"
                        style="width:240px;"
                        @keyup.native.enter="searchData"
                    ></el-input>
                </div>
            </div>
            <edu-table
                :selection="false"
                @page-change="pageChange"
                :data="tableData"
                :order="false"
                :total="total"
                :pageSize="pageSize"
                :pageNum="pageNum"
            >
                <el-table-column
                    :label="$t('minor.major.minorGrade')"
                    align="center"
                    min-width="80px"
                >
                    <template slot-scope="scope">{{scope.row.entranceTerm}}</template>
                </el-table-column>
                <el-table-column label="学号" show-overflow-tooltip align="center" min-width="80px">
                    <template slot-scope="scope">{{scope.row.studentId}}</template>
                </el-table-column>
                <el-table-column label="姓名" show-overflow-tooltip align="center" min-width="100px">
                    <template slot-scope="scope">{{scope.row.studentName}}</template>
                </el-table-column>
                <el-table-column
                    label="进阶层次"
                    show-overflow-tooltip
                    align="center"
                    min-width="100px"
                >
                    <template slot-scope="scope">{{scope.row.advancedLevelName}}</template>
                </el-table-column>
                <el-table-column label="学院" show-overflow-tooltip align="center" min-width="140px">
                    <template slot-scope="scope">{{scope.row.departmentIdI18n}}</template>
                </el-table-column>
                <el-table-column label="专业" show-overflow-tooltip align="center" min-width="140px">
                    <template slot-scope="scope">{{scope.row.majorNameCn}}</template>
                </el-table-column>
                <el-table-column
                    label="关联方案"
                    show-overflow-tooltip
                    align="center"
                    min-width="300px"
                >
                    <template slot-scope="scope">{{scope.row.cultureName}}</template>
                </el-table-column>
                <el-table-column
                    label="开始时间"
                    show-overflow-tooltip
                    align="center"
                    min-width="120px"
                >
                    <template slot-scope="scope">{{scope.row.showStartTime}}</template>
                </el-table-column>
                <el-table-column
                    label="结束时间"
                    show-overflow-tooltip
                    align="center"
                    min-width="120px"
                >
                    <template slot-scope="scope">{{scope.row.showEndTime}}</template>
                </el-table-column>
                <el-table-column label="状态" show-overflow-tooltip align="center" min-width="100px">
                    <template slot-scope="scope">
                        <el-tag
                            type="success"
                            v-if="scope.row.statusPlan == 3"
                        >{{scope.row.statusPlanName}}</el-tag>
                        <el-tag
                            type="warning"
                            v-else-if="scope.row.statusPlan == 2||scope.row.statusPlan == 4"
                        >{{scope.row.statusPlanName}}</el-tag>
                        <el-tag type="info" v-else>{{scope.row.statusPlanName}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column :label="$t('message.operating')" align="center" min-width="100px">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            v-if="scope.row.statusPlan==0||scope.row.statusPlan==1||scope.row.customFlag"
                            @click="customPlan(scope)"
                            size="mini"
                        >自定义计划</el-button>
                        <el-button v-else type="text" @click="viewPlan(scope)" size="mini">查看计划</el-button>
                    </template>
                </el-table-column>
            </edu-table>
        </el-card>
    </div>
</template>

<script>
import UTILS from "common/src/utils/utils";
import { getSessionUser } from "common/src/api/session";
import { queryDic } from "common/src/api/dictionary";
import { findMinorStudentCultureRelList } from "common/src/api/minor/minorCulturePlanManager";
import { queryAdvancedLevel } from "common/src/api/minor/collegemajor";
import { queryProfessionByCollege } from "common/src/api/minor/minorCultureScheme";
export default {
    name: "PlanManagerList",
    data() {
        return {
            canSelectFaculty: true,
            queryParams: {
                entranceTerm: "",
                departmentId: "",
                major: "",
                studentName: "",
                startTime: "",
                endTime: ""
            },
            oldQueryParams: {},
            options: {
                college: [],
                majorName: [],
                cultureStatus: [],
                advancedLevel: []
            },
            tableData: [],
            total: 0,
            pageNum: 1,
            pageSize: 20
        };
    },
    mounted() {
        if (!this.$checkMinor()) return;
        this.getOptions();
        this.getAdvancedLevel();
        //this.getTemplate();
    },
    methods: {
        //获取当前登录角色权限信息
        getType() {
            getSessionUser().then(res => {
                if ((res.code = 200)) {
                    //校管理员登录
                    if (
                        res.data.user.innerRoles.join(",").indexOf("1") >= 0 &&
                        res.data.user.currentRole == "1"
                    ) {
                        //学院不可选择
                        this.canSelectFaculty = true;
                        this.queryParams.departmentId = "";
                        this.getTemplate();
                        //院教务员登录
                    } else if (
                        res.data.user.innerRoles.join(",").indexOf("2") >= 0 &&
                        res.data.user.innerRoles.join(",").indexOf("1") == -1 &&
                        res.data.user.currentRole == "1"
                    ) {
                        //学院不可选择
                        this.canSelectFaculty = false;
                        this.queryParams.departmentId = this.options.college[0].value;
                        this.queryParams.departmentIdI18n = this.options.college[0].value;
                        this.majorNameChange();
                        this.searchData();
                    }
                }
            });
        },
        searchData() {
            this.oldQueryParams = Object.assign({}, this.queryParams);
            if (this.oldQueryParams.startTime) {
                this.oldQueryParams.startTime = moment(
                    this.oldQueryParams.startTime
                ).valueOf();
            }
            if (this.oldQueryParams.endTime) {
                this.oldQueryParams.endTime = moment(
                    this.oldQueryParams.endTime
                ).valueOf();
            }
            this.getTemplate();
        },
        getTemplate() {
            if (!this.$checkMinor()) return;
            let data = {
                projId: 1,
                pageNum_: this.pageNum,
                pageSize_: this.pageSize,
                statusRelation: 2
            };
            let params = Object.assign({}, data, this.oldQueryParams);
            findMinorStudentCultureRelList(params).then(({ code, data }) => {
                if (code === 200) {
                    this.tableData = data.list.map(el => {
                        el.showStartTime = el.startTime
                            ? moment(el.startTime).format("YYYY-MM-DD")
                            : "";
                        el.showEndTime = el.endTime
                            ? moment(el.endTime).format("YYYY-MM-DD")
                            : "";
                        if (el.statusPlan == "3") {
                            el.statusPlanName = "通过";
                        } else if (el.statusPlan == "2") {
                            el.statusPlanName = "审核中";
                        } else if (el.statusPlan == "1") {
                            el.statusPlanName = "草稿";
                        } else if (el.statusPlan == "4") {
                            el.statusPlanName = "未通过";
                            if (el.endTime < moment().valueOf()) {
                                el.customFlag = false;
                            } else {
                                el.customFlag = true;
                            }
                        } else {
                            el.statusPlanName = "未定义";
                        }
                        return el;
                    });
                    this.total = data.total_;
                }
            });
        },
        getOptions() {
            let data = {
                lang: this.$store.getters.language || "cn",
                type: "allChild",
                keys: ["X_YX"]
            };
            queryDic(data).then(res => {
                this.options.college = [];
                UTILS.fillSelect(
                    {
                        college: "X_YX"
                    },
                    res.data,
                    this.options
                );
                this.getType();
            });
        },
        getAdvancedLevel() {
            let params = {
                isOnCampus: "0"
            };
            this.options["advancedLevel"] = [];
            queryAdvancedLevel(params).then(res => {
                if (res.code == 200) {
                    for (let key = 0; key < res.data.length; key++) {
                        this.options["advancedLevel"].push({
                            value: res.data[key].advancedLevelCode,
                            label: res.data[key].advancedLevelName
                        });
                    }
                }
            });
        },
        majorNameChange() {
            let params = {
                collegeCode: this.queryParams.departmentIdI18n
            };
            queryProfessionByCollege(params).then(res => {
                if (res.code == 200) {
                    this.options.majorName = res.data.map(el => {
                        return el;
                    });
                }
            });
        },
        //重置查询条件
        resetQuery() {
            this.queryParams.entranceTerm = "";
            this.queryParams.advancedLevelCode = "";
            if (this.canSelectFaculty) {
                this.queryParams.departmentId = "";
            }
            this.queryParams.major = "";
            this.queryParams.startTime = "";
            this.queryParams.endTime = "";
            this.searchData();
        },
        viewPlan(payload) {
            if (!this.$checkMinor()) return;
            let { row } = payload;
            this.$emit(
                "view-plan",
                row.studentId,
                row.entranceTerm,
                false,
                "viewPlan"
            );
        },
        customPlan(payload) {
            if (!this.$checkMinor()) return;
            let { row } = payload;
            if (moment(row.endTime) < moment().valueOf()) {
                this.$message({
                    type: "warning",
                    message: "自定义计划时间已过期!"
                });
                return;
            }
            if (moment(row.startTime) > moment().valueOf()) {
                this.$message({
                    type: "warning",
                    message: "自定义计划时间未开始!"
                });
                return;
            }
            this.$emit(
                "custom-plan",
                row.studentId,
                row.entranceTerm,
                true,
                "customPlan"
            );
        },
        pageChange({ pageSize, pageNum }) {
            this.pageSize = pageSize;
            this.pageNum = pageNum;
            this.getTemplate();
        }
    }
};
</script>

<style scoped>
.title {
    font-family: PingFangSC-Medium;
    font-size: 16px;
    color: #2b3b4e;
    letter-spacing: 0;
    line-height: 20px;
    margin-bottom: 16px;
}
</style>
